<?php
global $base_url;
global $user;
$image_path = $base_url . '/' . imagecache_create_path('organization_image', $node->field_org_image[0]['filename']);

$support_message = "";
if (isset($_SESSION['referral_uid'])) {
    $user_profile = content_profile_load('profile', $_SESSION['referral_uid']);
    $student_name = $user_profile->field_firstname[0]['value'] . ' ' . $user_profile->field_lastname[0]['value'];
    $support_message = "<span class='student_surpport_message'>, Thanks for the support to $student_name</span>";
}
$org_id = arg(1);
if($org_id != NULL && is_numeric($org_id)){
    $organization = node_load($org_id);
    if ($organization->type == 'organization' && $user->uid == false){
        $_SESSION['organization_join'] = $org_id;
    }
}
$campaigns = db_query('SELECT * FROM {content_type_sale_campaign}, {node} 
    WHERE content_type_sale_campaign.field_campaign_org_nid = %d
    AND content_type_sale_campaign.nid = node.nid', $node->nid);

$campaign_list = array();

while ($c = db_fetch_object($campaigns)) {
    $from = explode("T", $c->field_duration_value);
    $from_date = $from[0];
    $to = explode("T", $c->field_duration_value2);
    $to_date = $to[0];
    $from_year = ((int) substr($from_date, 0, 4));
    $from_month = ((int) substr($from_date, 5, 2));
    $from_day = ((int) substr($from_date, 8, 2));
    $from_timestamp = mktime(0, 0, 0, $from_month, $from_day, $from_year);

    $to_year = ((int) substr($to_date, 0, 4));
    $to_month = ((int) substr($to_date, 5, 2));
    $to_day = ((int) substr($to_date, 8, 2));
    $to_timestamp = mktime(0, 0, 0, $to_month, $to_day, $to_year);
    if (time() >= $from_timestamp && time() <= $to_timestamp) {
        $is_no_going = true;
        $campaign_title = $c->title;
        $start_time = $from_date;
        $end_time = $to_date;
        $cam_gole = $c->field_compaign_goal_value;
        $campid = $c->nid;
    } else {
        //push the past due campain into $campaign list
        array_push($campaign_list, $c);
    }
    if ($start_time == NULL) {
        $begin_time = '';
    } else {
        $begin_time = date("M d Y", strtotime("$start_time"));
    }

    if ($end_time == NULL) {
        $finish_time = '';
    } else {
        $finish_time = date("M d Y", strtotime("$end_time"));
    }
}

$join_html = drupal_get_form('core101_user_customization_org_join_form');
?>
<section class="organization_info">
    <img src="<?php echo $image_path; ?>" />
    <h2>
        <?php print $node->title . $support_message; ?>
    </h2>
    <?php if ($is_no_going) : ?>
        <h3><?php print $campaign_title; ?></h3>
        <h3 class="red"><?php print 'Our Goal - $' . $cam_gole ?></h3>
        <?php if ($user->uid && _is_in_organization($user, $node->nid)) : ?>
            <?php print "Your Referral Code:" . core101_user_customization_encrypt($user->uid . '_' . $campid); ?>
        <?php endif; ?>
    <?php endif; ?>
    
    <div id="join-wrapper">
        
		<div id="joinOrgWarper">
        <?php
        $reg_link = url('user/register', array('absolute' => true));
        $log_link = url('user/login',  array('absolute' => true));
        $log_link .= '?destination=node/' . arg(1);
        $reg_link .= '?destination=node/' . arg(1);
        $isMember = false;
        if ($node->type == 'organization') {
            $profile = content_profile_load('profile', $user->uid);
            if ($profile != null) {
                foreach ($profile->field_profile_org as $val) {
                    if ($val['nid'] == $node->nid) {
                        $isMember = true;
                        break;
                    }
                }
            }
        }
        if (!$isMember && isset($_GET["referral_code"]) == false) :
            if($user->uid == FALSE) : ?>
                <div class="popup-wrapper">
                	<div id="login-button" class="push-button" >
					<a href="<?php echo $log_link;?>">Login</a>
					</div>
					<div class="popup-text">
					<span>*Login here if you already have an account.</span>
					</div>
				</div>
				<div class="popup-wrapper">	
					<div id="register-button" class="push-button">
			<a href="<?php echo $reg_link;?>">Register</a>
		</div>
					<div class="popup-text">
						<span>*Register a new account</span>
					</div>
				</div>
			
			<?php endif;?>
			    <div class="popup-wrapper">
			<?php print $join_html; ?>
            	<div class="popup-text" id="popup-join">
       					<span>*Join a campaign. (You do not need to join in order to shop)</span>
       				</div>
       			</div>
            <?php endif;
       		?>
       				
    	</div>	
	</div>
	<div id="organization-body">
    <p><?php print $node->content['body']['#value'] ?></p>
	</div>
</section>
